package main

import (
	"fmt"
	"math/rand"
	"sync"
	"time"
)

func main(){
	cont := sync.NewCond(&sync.Mutex{})
	msg1 := make(chan string)	//市场监督局下发任务
	msg2 := make(chan string)	//小道消息
	var danger bool = false
	for i:=1; i<5; i++{
		go func(index int){
			for{
				cont.L.Lock()
				if danger{
					fmt.Println("露天烧烤摊--",index,"躲起来")
				}else{
					fmt.Println("露天烧烤摊--",index,"开业")
				}
				cont.Wait()
				fmt.Println("-----------------------------------------")
				cont.L.Unlock()
				time.Sleep(time.Second*3)
			}
		}(i)
	}

	/*
	市场监督局
	 */
	go func(){
		for{
			r := rand.New(rand.NewSource(time.Now().UnixNano()))
			if r.Intn(999)%2 == 0{
				msg1<-"城管出发去检查"
				fmt.Println("市场监督局下发通知让城管检查露天商贩")
			}else{
				//fmt.Println("市场监督局在喝茶")
			}
			time.Sleep(time.Second*6)
		}
	}()

	/**
		消息监听的人
	 */
	go func(){
		for{
			select {
			case <-msg2:
				cont.L.Lock()
				danger = true
				cont.Broadcast()
				cont.L.Unlock()
			default:
				time.Sleep(time.Second*1)
			}
		}
	}()
	/**
	城管大队
	 */
	go func(){
		for{
			select {
			case <-msg1:
				msg2<-"城管出动了，大家躲起来"
				fmt.Println("城管开始检查")
			default:
				cont.L.Lock()
				danger = false
				cont.Broadcast()
				cont.L.Unlock()
				//fmt.Println("城管大队在办公室休息...")
			}
			time.Sleep(time.Second*3)
			fmt.Println("城管检查完毕")
		}
	}()
	time.Sleep(time.Second*365)
}